var express = require("express");
var router = express.Router();
var db = require("../db/index");

// 校验学号是否存在
router.get("/stucode", function (req, res) {
  let {stu_code} = req.query;
  db.query(`SELECT stu_code from account WHERE stu_code = '${stu_code}'`, function (err, row) {
    console.log(row)
    if (row.length > 0) {
      res.send({
        code: 4001,
        message: "重复"
      })
    } else {
      res.send({
        code: 200,
        message: '无重复'
      })
    }
  })
})

// 注册接口 post
router.post("/register", function (req, res, next) {
  var {stu_code, name, password, passProtect, passProtectAnswer} = req.body; // 获取用户名和密码
  db.query(
      "INSERT INTO account set ? ",
      {stu_code, name, password, pass_protect: passProtect, pass_protect_answer: passProtectAnswer},
      function (err) {
        if (err) {
          res.send({
            code: 4002,
            message: err.message
          })
        } else {
          res.send({
            code: 200,
            message: '成功'
          });
        }
      }
  );
});

// 登录接口 post
router.post("/login", function (req, res, next) {
  let {stu_code, password} = req.body;
  db.query(
      `select a.*,c.title as society_title from account a LEFT JOIN society c ON a.society_id = c.id where stu_code = '${stu_code}' and password = '${password}'`,
      function (err, rows) {
        if (rows.length > 0) {
          res.send({
            code: 200,
            userinfo: {
              ...rows[0],
              society_id: rows[0].society_id || '0'
            },
          });
        } else {
          res.send({
            code: 304,
            message: "登录失败",
          });
        }
      }
  );
});

router.get("/info", function (req, res, next) {
  let {id} = req.query;
  db.query(
      `SELECT a.*,c.title AS society_title FROM account a LEFT JOIN society c ON a.society_id = c.id WHERE a.id = ${id}`,
      function (err, rows) {
        if (rows.length > 0) {
          res.send({
            code: 200,
            userinfo: {
              ...rows[0],
              society_id: rows[0].society_id || '0'
            },
          });
        } else {
          res.send({
            code: 30004,
            message: "失败",
          });
        }
      }
  );
});

// 改密接口 post
router.post('/changePass', function (req, res) {
  let {stu_code, oldPassword, newPassword} = req.body;
  db.query(`UPDATE account SET password = ? WHERE stu_code = ${stu_code} AND password = ${oldPassword}`, [newPassword], function (err, row) {
    if (row.affectedRows != 0) {
      res.send({
        code: 200,
        message: '成功'
      })
    } else {
      res.send({
        code: 20085,
        message: '失败'
      })
    }
  })
})

// 找回密码 post
router.post('/forgetPass', function (req, res) {
  let {stu_code, passProtect, answer, newPassword} = req.body;
  db.query(`UPDATE account SET password = ? WHERE stu_code = ${stu_code} AND pass_protect = '${passProtect}' AND pass_protect_answer=${answer}`, [newPassword], function (err, row) {
    if (row.affectedRows != 0) {
      res.send({
        code: 200,
        message: '成功'
      })
    } else {
      res.send({
        code: 20085,
        message: '失败'
      })
    }
  })
})


// 修改密码 post
router.post("/upPass", function (req, res, next) {
  let {id, password} = req.body;
  db.query(
      `UPDATE account SET password = ? WHERE id = '${id}'`,
      [password],
      function (err, rows) {
        if (err) {
          res.json({
            errno: 21,
            message: "密码修改失败",
          });
        } else {
          res.json({
            errno: 0,
          });
        }
      }
  );
});

module.exports = router;
